<template>
  <div class="top">
    <div class="title">{{ item.name }}</div>
    <div class="manager">{{ item.manager }}</div>
    <el-dropdown @command="commandEvent">
      <span>
        操作
        <i class="el-icon-arrow-down el-icon--right" />
      </span>
      <template #dropdown>
        <el-dropdown-menu>
          <el-dropdown-item command="add">添加子部门</el-dropdown-item>
          <el-dropdown-item
            v-if="!item.isTop"
            command="edit"
          >查看部门</el-dropdown-item>
          <el-dropdown-item
            v-if="!item.isTop"
            command="del"
          >删除部门</el-dropdown-item>
        </el-dropdown-menu>
      </template>
    </el-dropdown>
  </div>
</template>

<script>
import { companyDepartmentDelete } from '@/api/departments'
export default {
  props: {
    // 当前点击项的数据
    item: {
      required: true,
      type: Object,
      default: () => ({})
    }
  },
  methods: {
    async commandEvent(command) {
      switch (command) {
        case 'add':
          // 新增
          this.$bus.$emit('openAdd', true, this.item, 'add')
          break
        case 'del':
          // 删除
          await this.$confirm('你确定删除该部门吗?', '提示')
          await companyDepartmentDelete(this.item.id)
          // 提示
          this.$message.success('删除成功')
          // 刷新父级列表数据
          this.$emit('getData')
          break
        case 'edit':
          // 编辑
          this.$bus.$emit('openAdd', true, this.item, 'edit')
          break
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.top {
  display: flex;
  justify-content: space-between;
  width: 100%;
  .title {
    flex: 1;
  }
  .manager {
    margin-right: 30px;
    text-align: right;
  }
}
</style>
